package com.jackrain.nea.ext.permission;

import com.alibaba.druid.sql.visitor.functions.Char;
import com.jackrain.nea.core.schema.Table;
import com.jackrain.nea.core.schema.TableManager;
import lombok.extern.slf4j.Slf4j;

import javax.management.relation.RelationSupport;

/**
 * @author lzm
 * @date 2017-11-18
 */
@Slf4j
public class TableMaskHelper {
    public static String ONLY_VIEW_MASK = "10000000";
    public static Integer ALL_PERMISSIONS = 0b11111111;
    public static Integer ALL_ACTION_PERMISSIONS = 0b10000000;
    private static String oldMask = "QADMSPGUV";   // 9 elements, means QADMSPGUV by order
    private static String newMask = "QMDVSUEP";
    //Q[AM]DVSUQQ
    private static String[] map2OldMasks = new String[]{"Q", "AM", "D", "V","S", "U", "Q", "Q"};
    private static String[] temp = new String[]{"查看", "编辑", "删除", "作废", "提交", "反提交", "导出", "打印"};
    public static int generateMask(Table table){
        int result = 0;
        for (String mapValue : map2OldMasks){
            int value = 0;
            for (char c: mapValue.toCharArray()){
                value |= table.isActionEnabled(oldMask.indexOf(c))?1:0;
            }
            result = result <<1+ value;
        }
        return result;
    }

    public static String generateMaskString(Table table){
        StringBuffer sb = new StringBuffer();
        for (String mapValue : map2OldMasks){
            Integer value = 0;
            for (char c: mapValue.toCharArray()){
                value |= table.isActionEnabled(oldMask.indexOf(c))?1:0;
            }
            sb.append(value == 1 ? "1":"0");
        }
        return sb.toString();
    }

    public static boolean maskIsSet(String maskStr, Integer permission) {
        int index =newMask.indexOf(maskStr);
        if(index <0) {
            return false;
        }
        int mask = 1 << (newMask.length()-1 - index);
        return (permission & mask) == 1;
    }

    public static String union(String left, String right) {
        Integer result = TableMaskHelper.convertToInt(left) | TableMaskHelper.convertToInt(right);
        return getNewMaskString(result);
    }

    //"QMD"
    public static int fromNewMaskString(String mask){
        int result = 0;
        char[] chars = mask.toCharArray();
        for (char c :chars){
            int index =newMask.indexOf(c);
            result = result |(1 << (newMask.length()-1 - index));
        }
        return result;
    }

    //"11000011"
    public static int convertToInt(String newMaskString){
        if(newMaskString == null || newMaskString.isEmpty()){
            return 0;
        }
        try {
            return ALL_PERMISSIONS & Integer.parseInt(newMaskString, 2);
        }
        catch (Exception e){
            log.warn(e.getMessage());
            return 0;
        }
//        int result = 0;
//        char[] chars = newMaskString.toCharArray();
//        int index =0;
//        for (char c :chars){
//
//            result = result |((c=='1'?1:0) << (newMask.length()-1 - index));
//            index ++;
//        }
//        return result;
    }

    public static String getNewMaskString(Integer mask){
        StringBuffer sb = new StringBuffer();
        Integer length = newMask.length();
        for (int i=0; i < length; i++){
            int result = mask % 2;
            sb.insert(0, (result==1)?"1":"0");
        }
        return sb.toString();
    }



    public static String generateActionMask(TableManager tm, MenuCollection tree, Long actionId){
        Long tableId =tree.fetchTableIdByAliasAction(actionId);
        if(tableId != null && tableId > 0){
            Table table = tm.getTable(tableId.intValue());
            if(table != null){
                return TableMaskHelper.generateMaskString(table);
            }
            //tree.fetchByTmId(MenuType.TABLE, )
        }
        return ONLY_VIEW_MASK;
    }
}
